package com.ibm.spatiotemp.datamodel.datatype.request;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.spatiotemp.tools.jsonParser;

import edu.umn.cs.spatialHadoop.core.Shape;

public class QueryRequest extends Request {

	/* define what tags is contained */
	// public static String[] tags = {"qtype","k"};
	// public static Class[] tags_class = { String.class, Integer.class };
	
	public final static String IDXLAYER = "idxlayer";
	public final static String QUERYTYPE = "querytype";
	public final static String STARTTS = "starttss";
	public final static String ENDTS = "endts";
	public final static String MODIDS = "modids";
	public final static String K = "k";
	public final static String OUTSCHEMA = "outschema";
	
	public String json_str;
	private JsonNode jsonnode;
	
	/*target index layer to search with*/
	public String idxlayer;
	
	public String querytype;
	public long startts;
	public long endts;

	/* geojson format */
	public String qshape_json;
	private Shape qshape;
	public String[] modids;
	public int k;
	
	/*
	 * json format: define what was output, i.e., movid,position,timerange...,or
	 * just some of them
	 */
	public String outschema;

	public QueryRequest() {
		startts = -1;
		endts = -1;
	}
	
	public QueryRequest(String jsonstr) {
		this.json_str = jsonstr;
		jsonnode = jsonParser.fromString(jsonstr);
		
		/*target index layer to search with*/
		idxlayer = getTagAsString("idxlayer");
		querytype = getTagAsString("querytype");
		startts = getTagAsLong("starttss");
		endts = getTagAsLong("endts");
		qshape_json = getTagAsString("qshape_json");		/* geojson format */
		modids = jsonParser.GetArray("modids", jsonnode);
		outschema = getTagAsString("outschema");/*json format*/
//		k = getTagAsInteger("k");
	}
	
	public QueryRequest clone(){
		QueryRequest result = new QueryRequest();
		result.json_str = json_str;
		if(this.jsonnode==null)
			result.jsonnode = jsonParser.fromString(json_str);
		else
		{
			String res = this.jsonnode.toString();
			result.jsonnode = jsonParser.fromString(res);
		}
		/*target index layer to search with*/
//		result.idxlayer = getTagAsString("idxlayer");
//		result.querytype = getTagAsString("querytype");
//		result.startts = getTagAsLong("starttss");
//		result.endts = getTagAsLong("endts");
//		result.qshape_json = getTagAsString("qshape_json");		/* geojson format */
//		result.modids = jsonParser.GetArray("modids", jsonnode);
//		result.outschema = getTagAsString("outschema");/*json format*/
//		k = getTagAsInteger("k");
		result.datasrcid = this.datasrcid;
		result.idxlayer = this.idxlayer;
		result.querytype = this.querytype;
		result.startts = this.startts;
		result.endts = this.endts;
		result.qshape_json = this.qshape_json;		/* geojson format */
		result.modids = this.modids;
//		if(this.modids!=null)
//		result.modids = (String[]) Arrays.copyOf(this.modids, this.modids.length);
 		result.outschema = this.outschema;
		
		return result;
	}

	public void setParameter(String paraname, Object value){
		if(jsonnode==null){
			if(json_str!=null){
				jsonnode = jsonParser.fromString(json_str);
			}
			else{
				jsonnode = jsonParser.newObjectNode(); 
			}
		}
		ObjectNode jsonnode_obj = (ObjectNode) jsonnode;
		jsonnode_obj = jsonnode_obj.putPOJO(paraname, value);
	}
	
//	public Object getParameter(String paraname,Class<?> classobj){
//		ObjectNode jsonnode_obj = (ObjectNode) jsonnode;
//		classobj.cast(obj)
//		jsonnode_obj.get(paraname).;
//	}
	
	public String getIdxlayer() {
		return idxlayer;
	}

	public void setIdxlayer(String idxlayer) {
		this.idxlayer = idxlayer;
	}

	public String getTagAsString(String tagname) {
		return jsonParser.GetString(tagname, jsonnode, null);
	}

	public int getTagAsInteger(String tagname) {
		return jsonParser.GetInteger(tagname, jsonnode, -1);
	}
	
	public long getTagAsLong(String tagname)
	{
		return jsonParser.GetLong(tagname,	jsonnode, -1);
	}

	public Shape getQshape() {
		if (qshape == null)
			qshape = jsonParser.getShapefromJsonNode(jsonParser
					.fromString(qshape_json));
		return qshape;
	}

	public String getQshape_json() {
		return qshape_json;
	}

	public void setQshape_json(String qshape_json) {
		this.qshape_json = qshape_json;
	}

	public String[] getModids() {
		if (modids == null && jsonnode!=null)
			modids = jsonParser.GetArray("modids", jsonnode);
		return modids;
	}

	public void setModids(String[] modids) {
		this.modids = modids;
	}


	public String getQuerytype() {
		return querytype;
	}

	public void setQuerytype(String querytype) {
		this.querytype = querytype;
	}

	public long getStartts() {
		if (startts == -1)
			startts = jsonParser.GetLong("startts", jsonnode, -1);
		return startts;
	}

	public void setStartts(long startts) {
		this.startts = startts;
	}

	public long getEndts() {
		if (endts == -1)
			endts = jsonParser.GetLong("endts", jsonnode, -1);
		return endts;
	}

	public void setEndts(long endts) {
		this.endts = endts;
	}

	public String getOutschema() {
		return outschema;
	}

	public void setOutschema(String outschema) {
		this.outschema = outschema;
	}

}
